package otxImporter;

import gmmxEditor.Size;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.Vector;

public class Object implements OTXImportable {

	public static int		STATE_DEFAULT			= 0;
	public static int		STATE_BROKEN_PARTIAL	= 1;

	private int				tileX;
	private int				tileY;
	private int				width;
	private int				height;
	private BufferedImage	img;

	private int				id;
	private int				state;

	private Vector<Object>	states;

	@Override
	public void setTile(int x, int y, int width, int height, BufferedImage img, int id, int state) {
		this.tileX = x;
		this.tileY = y;
		this.img = img;
		this.id = id;
		this.state = state;
		this.width = width;
		this.height = height;
		this.states = new Vector<Object>();
	}

	@Override
	public String toString() {
		return "ID: " + this.id + " (" + this.state + ")\n    x: " + this.tileX + "\n    y: " + this.tileY + "\n    width: " + this.width + "\n    height: "
				+ this.height;
	}

	@Override
	public int getX() {
		return this.tileX;
	}

	@Override
	public int getY() {
		return this.tileY;
	}

	@Override
	public int getWidth() {
		return this.width;
	}

	@Override
	public int getHeight() {
		return this.height;
	}

	@Override
	public int getState() {
		return this.state;
	}

	public Object getStateObject(int state) {
		if (this.state == state) {
			return this;
		}

		for (Object o : this.states) {
			if (o.getState() == state) {
				return o;
			}
		}
		return null;
	}

	@Override
	public void addState(OTXImportable obj) {
		this.states.add((Object) obj);
	}

	@Override
	public BufferedImage getImage() {
		return this.getImage(STATE_DEFAULT);
	}

	@Override
	public BufferedImage getImage(int state) {
		Object obj = this.getStateObject(state);

		return this.img.getSubimage(obj.getX(), obj.getY(), obj.getWidth(), obj.getHeight());
	}

	@Override
	public boolean equals(java.lang.Object obj) {
		if (obj instanceof OTXImportable) {
			return ((OTXImportable) obj).getId() == this.id;
		}
		return false;
	}

	@Override
	public int getId() {
		return this.id;
	}

	@Override
	public Size getSize() {
		return new Size(this.width, this.height);
	}

	@Override
	public Rectangle getRect() {
		return new Rectangle(this.tileX, this.tileY, this.width, this.height);
	}

}
